package com.wqiuqiu.gulimall.product.service.impl;

import com.wqiuqiu.gulimall.product.vo.SpuAttrVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wqiuqiu.common.PageUtils;
import com.wqiuqiu.common.Query;

import com.wqiuqiu.gulimall.product.dao.ProductAttrValueDao;
import com.wqiuqiu.gulimall.product.entity.ProductAttrValueEntity;
import com.wqiuqiu.gulimall.product.service.ProductAttrValueService;
import org.springframework.transaction.annotation.Transactional;


@Service("productAttrValueService")
public class ProductAttrValueServiceImpl extends ServiceImpl<ProductAttrValueDao, ProductAttrValueEntity> implements ProductAttrValueService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ProductAttrValueEntity> page = this.page(
                new Query<ProductAttrValueEntity>().getPage(params),
                new QueryWrapper<ProductAttrValueEntity>()
        );

        return new PageUtils(page);
    }

    @Transactional
    @Override
    public void updateSpuAttr(List<SpuAttrVo> attr, Long spuId) {
        //如果这个属性是新增，也就是数据库中没有该记录,或者取消了一些属性
        //一种解决办法，可以将spuId绑定的全部属性先删除，然后重新插入提交的属性
        List<ProductAttrValueEntity> newProductAttrValueEntities = new ArrayList<>();
        List<ProductAttrValueEntity> oldProductAttrValueEntities = new ArrayList<>();
        int count = this.count(new QueryWrapper<ProductAttrValueEntity>().eq("spu_id",spuId));
        List<Long> attrIds = new ArrayList<>();
        for (SpuAttrVo obj : attr) {
            ProductAttrValueEntity one = this.getOne(new QueryWrapper<ProductAttrValueEntity>().eq("spu_id", spuId).eq("attr_id", obj.getAttrId()));
            ProductAttrValueEntity attrValueEntity = new ProductAttrValueEntity();
            attrValueEntity.setSpuId(spuId);
            BeanUtils.copyProperties(obj,attrValueEntity);
            attrIds.add(obj.getAttrId());
            if (one == null) {
                newProductAttrValueEntities.add(attrValueEntity);
            } else {
                Long id = one.getId();
                BeanUtils.copyProperties(attrValueEntity,one);
                one.setId(id);
                oldProductAttrValueEntities.add(one);
            }
        }
        if (oldProductAttrValueEntities.size() > 0) {
            this.updateBatchById(oldProductAttrValueEntities);
        }
        if (newProductAttrValueEntities.size() > 0) {
            this.saveBatch(newProductAttrValueEntities);
        }
        if (count > attr.size()){
            //说明有属性被删除
            List<ProductAttrValueEntity> productAttrValueEntities = this.list(new QueryWrapper<ProductAttrValueEntity>().eq("spu_id", spuId));
            List<Long> removIds = productAttrValueEntities.stream().filter(obj -> {
                return !attrIds.contains(obj.getAttrId());
            }).map(obj->{
                return obj.getId();
            }).collect(Collectors.toList());
            this.removeByIds(removIds);
        }
    }

}